import { ref } from 'vue';
import { defineStore } from 'pinia';
import * as UserService from '../api/user';

type User = {
  id: number;
  memberNo: string;
  groupId: number;
  avatar: string;
  sex: number;
  firstName: string;
  lastName: string;
  fullName: string;
  countryCode: string;
  phoneNumber: string;
  birthday: string;
  email: string;
  points: number;
  levelCard: string;
  levelName: string;
  levelBadge: string;
  staffId: number;
  channelId: number;
  rebateMode: string;
};

export const useUserStore = defineStore('user', () => {
  // 用户状态
  const state = ref<User>({
    fullName: '',
    avatar: '/static/icon_not_logged_in.png',
    sex: 1
  } as User);

  /**
   * 设置用户头像
   * @param res - 包含头像URL的响应对象
   */
  function setAvatar(res: any) {
    state.value.avatar = res.detail.avatarUrl;
  }

  /**
   * 设置用户昵称
   * @param value
   */
  function setFirstName(value: string) {
    state.value.firstName = value;
  }

  /**
   * 设置用户昵称
   * @param value
   */
  function setLastName(value: string) {
    state.value.lastName = value;
  }

  /**
   * 设置用户昵称
   * @param value
   */
  function setFullName(value: string) {
    state.value.fullName = value;
  }

  /**
   * 设置用户昵称
   * @param value
   */
  function setNickName(res: any) {
    state.value.fullName = res.detail;
  }

  /**
   * 设置生日
   * @param value
   */
  function setBirthday(value: string) {
    state.value.birthday = value;
  }

  /**
   * 设置邮箱
   * @param value
   */
  function setEmail(value: string) {
    state.value.email = value;
  }

  /**
   * 设置区号
   * @param value
   */
  function setCountryCode(value: string) {
    state.value.countryCode = value;
  }

  /**
   * 设置手机
   * @param value
   */
  function setPhoneNumber(value: string) {
    state.value.phoneNumber = value;
  }

  /**
   * 设置性别
   * @param value
   */
  function setSex(value: number) {
    state.value.sex = value;
  }

  async function getUserInfo() {
    const res = await UserService.getUserInfo();
    if (res.code === 0) {
      state.value.firstName = res.data.firstName;
      state.value.lastName = res.data.lastName;
      state.value.fullName = res.data.fullName;
      state.value.sex = res.data.sex;
      state.value.points = res.data.balance;
      state.value.levelCard = res.data.levelInfo.levelCard;
      state.value.levelName = res.data.levelInfo.levelName;
      state.value.levelBadge = res.data.levelInfo.levelBadge;
      state.value.email = res.data.mail;
      state.value.countryCode = res.data.phoneArea;
      state.value.phoneNumber = res.data.phone;
      state.value.birthday = res.data.birthday ? res.data.birthday.split(' ')[0] : '';
      state.value.memberNo = res.data.memberNo;
      state.value.staffId = res.data.staffId;
      state.value.channelId = res.data.channelId;
      state.value.rebateMode = res.data.rebateMode;
      state.value.id = res.data.id;
      if (res.data.avatar) {
        state.value.avatar = res.data.avatar;
      } else {
        state.value.avatar =
          'https://apt11-1251002327.cos.ap-tokyo.myqcloud.com/miniapp/user_avatar_default.png';
      }
    }
  }

  async function join(data: any) {
    if (!data.firstName || !data.lastName) {
      uni.showToast({
        title: '请填写姓名',
        icon: 'none'
      });
      return;
    }

    if (data.sex === 0) {
      uni.showToast({
        title: '请选择性别',
        icon: 'none'
      });
      return;
    }

    if (!data.countryCode) {
      uni.showToast({
        title: '请选择国籍',
        icon: 'none'
      });
      return;
    }

    await UserService.setUserInfo({
      sex: data.sex,
      first_name: data.firstName,
      last_name: data.lastName,
      nationality: data.countryCode
    });

    await getUserInfo();

    uni.navigateBack({
      delta: 2
    });
  }

  return {
    user: state,
    setAvatar,
    setFirstName,
    setLastName,
    setFullName,
    setNickName,
    setBirthday,
    setEmail,
    setCountryCode,
    setPhoneNumber,
    setSex,
    getUserInfo,
    join
  };
});
